# download images from GEE
"""
reference:

[notebook: download image using geemap](https://geemap.org/notebooks/118_download_image/)

[notebook: download one big image parallel](https://geemap.org/notebooks/136_download_parallel/)

"""


# select bounds
import geemap
# mask research region

# set spatial resolution
# refer to https://spatialthoughts.com/2021/05/13/aggregating-population-data-gee/

# download to local



## download imageCollections
# ------------------------------------------------------------
# using geemap download images in imageCollection
# Input:
#   imgCols: image collection to download
#   init_path: dict, "dir_out" is the path to save images
#   scale: set the image resolution, unit: m
geemap.download_ee_image_collection(imgCols,init_path["dir_out"],scale = 1000)



## use miao alert to send notice to wechat
#-------------------------------------------------------
import os
from urllib import request, parse
import time
import json

# get total image number from above code cell
image_number = 7300
# miao code
miao_code = "tnLiHyP"

# const:
#   "miao": successful info
#   "miao_fail": fail info
if len(os.listdir("./data")) == image_number:
    text = init_const["miao"]
else:
    text = init_const["miao_fail"]

page = request.urlopen("http://miaotixing.com/trigger?" + parse.urlencode({"id":miao_code, "text":text, "type":"json"}))
result = page.read()
jsonObj = json.loads(result)
if (jsonObj["code"] == 0):
    print("成功")
else:
    print("失败，错误代码：" + str(jsonObj["code"]) + "，描述：" + jsonObj["msg"])